<?php

namespace App\Http\Controllers;

use App\Common\Func;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

class BaseController extends Controller
{
    /**
     * 验证器
     * @var
     */
    protected $validate;

    /**
     * 参数验证
     * @param $scene
     * @param null $params
     * @return array
     */
    function checkParam($scene, $params = null): array
    {
        $data = empty($params)
            ? request()->except('s')
            : request()->only($params);
        if (empty($scene)) {
            return $data;
        }
        if ($this->validate->check($data, [], [], $scene) !== true) {
            return Func::response('101', $this->validate->error);
        }
        return $data;
    }

    /**
     * api通用返回格式
     * @param int $code
     * @param string $message
     * @param array $data
     * @return JsonResponse
     */
    public function jsonResponse(string $code = '0', string $message = '', $data = null): JsonResponse
    {
        $returnData['version'] = '1.0';
        $returnData = array_merge($returnData,Func::response($code, $message, $data));
        return response()->json($returnData);
    }
}
